﻿package {
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author kot
	 * мяу
	 */
	public class  King extends Figure {
		
		override public function toString():String {
			return "King";
		}
		
		private var moved : Boolean = false;
		
		public function getMoved() : Boolean {
			return moved;
		}
		public function setMoved() : void {
			moved = true;
		}
		
		override public function getAvailableMoves(): int {
			resetMoves();
			for (var i = -1; i < 2; i++) {
				for (var j = -1; j < 2; j++) {
					if (Math.abs(i) + Math.abs(j) != 0) {
						var tmpX = figureX + i;
						var tmpY = figureY + j;
						if (checkCell(tmpX, tmpY)) {
							moves.push(new Point(figureX + i, figureY + j));
						}
					}
				}
			}
			if (moved || !Board(parent).positionOK()) {
				return moves.length;
			}
			var rook : Figure;
			if (figureColor == WHITE) {
				//длинная рокировка
				rook = Board(parent).getCell(0, 0).getFigure();
				if (rook != null && rook.toString() == "Rook" && rook.getFigureColor() == WHITE) {
					if (!((rook as Rook).isMoved()) && rook.canMove(3, 0)
					 && Board(parent).checkCellIsOk(new Point(3, 0), figureColor)
					 && Board(parent).checkCellIsOk(new Point(2, 0), figureColor)) {
						moves.push(new Point(2, 0));
					 }
				}
				// короткая рокировка
				rook = Board(parent).getCell(7, 0).getFigure();
				if (rook != null && rook.toString() == "Rook" && rook.getFigureColor() == WHITE) {
					if (!((rook as Rook).isMoved()) && rook.canMove(5, 0)
					 && Board(parent).checkCellIsOk(new Point(5, 0), figureColor)
					 && Board(parent).checkCellIsOk(new Point(6, 0), figureColor)) {
						moves.push(new Point(6, 0));
					 }
				}
			} else {
				rook = Board(parent).getCell(0, 7).getFigure();
				if (rook != null && rook.toString() == "Rook" && rook.getFigureColor() == BLACK) {
					if (!((rook as Rook).isMoved()) && rook.canMove(3, 7)
					 && Board(parent).checkCellIsOk(new Point(3, 7), figureColor)
					 && Board(parent).checkCellIsOk(new Point(2, 7), figureColor)) {
						moves.push(new Point(2, 7));
					 }
				}
				// короткая рокировка
				rook = Board(parent).getCell(7, 7).getFigure();
				if (rook != null && rook.toString() == "Rook" && rook.getFigureColor() == BLACK) {
					if (!((rook as Rook).isMoved()) && rook.canMove(5, 7)
					 && Board(parent).checkCellIsOk(new Point(5, 7), figureColor)
					 && Board(parent).checkCellIsOk(new Point(6, 7), figureColor)) {
						moves.push(new Point(6, 7));
					 }
				}
			}
			return moves.length;
		}
		
		public function updateKing(toX : int, toY : int) : void {
			if (figureColor == WHITE) {
				if (!moved && toX == 6 && toY == 0) {
					setMoved();
					Board(parent).getCell(5, 0).addFigure(Board(parent).getCell(7, 0).getFigure());
					Board(parent).getCell(7, 0).removeFigure();
					(Board(parent).getCell(5, 0).getFigure() as Rook).setMoved();
					this.setMoved();
				} else if (!moved && toX == 2 && toY == 0) {
					setMoved();
					Board(parent).getCell(3, 0).addFigure(Board(parent).getCell(0, 0).getFigure());
					Board(parent).getCell(0, 0).removeFigure();
					(Board(parent).getCell(3, 0).getFigure() as Rook).setMoved();
				} 
			} else {
				if (!moved && toX == 6 && toY == 7) {
					setMoved();
					Board(parent).getCell(5, 7).addFigure(Board(parent).getCell(7, 7).getFigure());
					Board(parent).getCell(7, 7).removeFigure();
					(Board(parent).getCell(5, 7).getFigure() as Rook).setMoved();
					this.setMoved();
				} else if (!moved && toX == 2 && toY == 7) {
					setMoved();
					Board(parent).getCell(3, 7).addFigure(Board(parent).getCell(0, 7).getFigure());
					Board(parent).getCell(0, 7).removeFigure();
					(Board(parent).getCell(3, 7).getFigure() as Rook).setMoved();
				} 
			}
		}
		override public function canMove(x : int, y : int) : Boolean {
			return (Math.abs(x - figureX) <= 1 && Math.abs(y - figureY) <= 1);
		}
	}
	
}